import { useEffect, useState, useImperativeHandle, forwardRef } from 'react';
import { connect } from 'umi';
import { Modal, Switch, Button, Message } from 'antd';
import { configModal } from '@/assets';
import { HttpCode } from '#/utils/contacts.js';

import styles from './index.less';

import { reConfigDatasource } from '@/pages/Tianji/DayRollingTradeStrategy/services';

function ForecastConflictModal(
  { menuCode, tradeSeqId, stationId, forceConflict, priceConflict, dispatch },
  ref,
) {
  useImperativeHandle(ref, () => ({
    initHandle,
  }));

  const [showModal, setShowModal] = useState(false),
    [forecastForce, setForecastForce] = useState(true),
    [forecastPrice, setForecastPrice] = useState(true);

  const initHandle = () => {
    setForecastForce(true);
    setForecastPrice(true);
    setShowModal(true);
  };

  const reConfigDatasourceAsync = async () => {
    dispatch({
      type: 'dayRollingTradeStragety/setAddLoadingNum',
    });
    let data = {
      sequenceId: tradeSeqId,
      unitId: stationId,
      menuCode: menuCode,
    };
    if (forceConflict) {
      data.forceReComputed = forecastForce;
    }
    if (priceConflict) {
      data.priceReComputed = forecastPrice;
    }
    const res = await reConfigDatasource(data);
    dispatch({
      type: 'dayRollingTradeStragety/setSubLoadingNum',
    });
    if (res?.statusCode === HttpCode.SUCCESS) {
      setShowModal(false);
      let forceDatasourceNotFind = res?.data?.forceDatasourceNotFind || false;
      let priceDatasourceNotFind = res?.data?.priceDatasourceNotFind || false;
      if (!forceDatasourceNotFind && !priceDatasourceNotFind) {
        Message.success('设置预测数据源成功');
      } else if (forceDatasourceNotFind && priceDatasourceNotFind) {
        Message.warning('当前交易单元不包含全局设置的出力预测源、现货价格预测源');
      } else if (forceDatasourceNotFind) {
        Message.warning('当前交易单元不包含全局设置的出力预测源');
      } else if (priceDatasourceNotFind) {
        Message.warning('当前交易单元不包含全局设置的现货价格预测源');
      }

      // 重新获取策略详情
      dispatch({
        type: 'dayRollingTradeStragety/fetchDetailEffect',
      });
    } else {
      Message.error(res.message || '设置预测数据源失败');
    }
  };

  return (
    <Modal
      className={styles.forecastConflictModal}
      centered={true}
      getContainer={false}
      title={
        <div className={styles.modalTitle}>
          <img src={configModal} />
          <span className={styles.text}>预测源冲突</span>
        </div>
      }
      visible={showModal}
      onCancel={() => {
        setShowModal(false);
      }}
      footer={[
        <Button key="submit" type="primary" onClick={reConfigDatasourceAsync}>
          提交
        </Button>,
      ]}
    >
      <div className={styles.tipMsg}>
        当前交易单元的预测源与全局设置的预测源有冲突，请选择预测源：
      </div>
      {forceConflict ? (
        <div className={styles.forecastItem}>
          <span className={styles.label}>出力预测</span>
          <div className={styles.item}>
            <Switch
              size="small"
              checked={forecastForce}
              onChange={(checked) => {
                setForecastForce(checked);
              }}
            />
            <span className={styles.text}>{forecastForce ? '全局预测源' : '交易单元预测源'}</span>
          </div>
        </div>
      ) : (
        ''
      )}
      {priceConflict ? (
        <div className={styles.forecastItem}>
          <span className={styles.label}>现货价格预测</span>
          <div className={styles.item}>
            <Switch
              size="small"
              checked={forecastPrice}
              onChange={(checked) => {
                setForecastPrice(checked);
              }}
            />
            <span className={styles.text}>{forecastPrice ? '全局预测源' : '交易单元预测源'}</span>
          </div>
        </div>
      ) : (
        ''
      )}
    </Modal>
  );
}

export default connect(
  ({ dayRollingTradeStragety, global }) => ({
    menuCode: global.configure.menuCode,
    tradeSeqId: dayRollingTradeStragety.tradeSeqId,
    stationId: dayRollingTradeStragety.stationId,
    forceConflict: dayRollingTradeStragety.forceConflict,
    priceConflict: dayRollingTradeStragety.priceConflict,
  }),
  null,
  null,
  { forwardRef: true },
)(forwardRef(ForecastConflictModal));
